package com.longxi.tianjintianbao.ui.base;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.utils.DrawableUtils;
import com.longxi.tianjintianbao.widget.EmptyLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public abstract class BaseDialogFragment<V, T extends BasePresenter<V>> extends DialogFragment implements IBaseView {
    /**
     * 把 EmptyLayout 放在基类统一处理，@Nullable 表明 View 可以为 null，详细可看 ButterKnife
     */
    @Nullable
    @BindView(R.id.tvLeft)
    protected TextView mTvLeft;
    @Nullable
    @BindView(R.id.ivLeft)
    protected ImageView mIvLeft;
    @Nullable
    @BindView(R.id.tvBarTitle)
    protected TextView mTvBarTitle;
    @Nullable
    @BindView(R.id.tvRight)
    public TextView mTvRight;
    @Nullable
    @BindView(R.id.ivRight)
    protected ImageView mIvRight;
    @Nullable
    @BindView(R.id.ivRight2)
    protected ImageView mIvRight2;

    @Nullable
    @BindView(R.id.empty_layout)
    protected EmptyLayout mEmptyLayout;

    protected T mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
        init();
        //判断是否使用MVP模式
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置dialog的 进出 动画
        getDialog().getWindow().setWindowAnimations(R.style.ActionSheetDialogAnimation);
        View rootView = inflater.inflate(provideContentViewId(), container, false);
        //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()
        ButterKnife.bind(this, rootView);
        initView(rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initListener();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    public void init() {

    }

    public void initView(View rootView) {
    }

    public void initData() {

    }

    public void initListener() {

    }

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    //用于创建Presenter和判断是否使用MVP模式(由子类实现)
    protected abstract T createPresenter();

    @Override
    public void showLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_LOADING);
        }
    }

    @Override
    public void hideLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void showNetError() {
    }

    @Override
    public void showNoData() {
    }

    @Override
    public void showError() {
    }

    @Override
    public void finishRefresh() {
    }

    @Override
    public void finishLoadMore() {
    }

    @Override
    public void setTitle(String title) {
        if(mTvBarTitle != null){
            mTvBarTitle.setVisibility(View.VISIBLE);
            mTvBarTitle.setText(title);
        }
    }

    @Override
    public void setTvLeft(String name) {
        if(mTvLeft != null){
            mTvLeft.setVisibility(View.VISIBLE);
            mTvLeft.setText(name);
            mTvLeft.setOnClickListener(v -> dismiss());
            DrawableUtils drawableUtils = new DrawableUtils();
            mTvLeft.setCompoundDrawables(drawableUtils.getDrawable(R.mipmap.ic_back, 2), null, null, null);
        }
    }

    @Override
    public void setIvLeft(int image) {

    }

    @Override
    public void setTvRight(String name) {
        if(mTvRight != null){
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText(name);
        }
    }

    @Override
    public void setIvRight(int image) {
        if(mIvRight != null){
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(image);
        }
    }

    @Override
    public void setIvRight2(int image) {

    }
}
